/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gui;

import com.app.game.chat.Message;
import com.app.game.other.Player;
import com.app.game.other.PlayerFix;
import com.app.observers.ServerObserver;
import java.util.Date;
import java.util.HashMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

/**
 *
 * @author Maciej Strychalski
 * @author Rafał Tomaszewicz
 */
public class ChatPanel implements ServerObserver{

    private VBox box;
    private TextField chatField;
    private TextArea chatArea;
    private Button sendButton;
    private Player player;
    
    public ChatPanel(Player player) {
        this.player = player;
        sendButton = new Button("Wyślij");
        sendButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                send();
            }
        });
        chatField = new TextField();
        chatField.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent e) {
                if(e.getCode()==KeyCode.ENTER){
                    send();
                }
            }
        });
        HBox.setHgrow(chatField, Priority.ALWAYS);
        HBox hb = new HBox(2);
        hb.getChildren().setAll(chatField,sendButton);
        box = new VBox(2);
        chatArea = new TextArea();
        chatArea.setMinHeight(60);
        chatArea.setEditable(false);
        chatArea.setFocusTraversable(false);
        chatArea.setWrapText(true);
        VBox.setVgrow(chatArea, Priority.ALWAYS);
        box.getChildren().setAll(chatArea,hb);
        //box.setPadding(new Insets(10));
    }
    public Node getChat(){
        return box;
    }
    private void send(){
        Message m = new Message();
        m.setPlayerName(player.getName());
        m.setTime(new Date());
        m.setMessageText(chatField.getText());
        player.sendMessage(m);
        chatField.clear();
    }

    @Override
    public void updatePlayer(Player player) {
       
    }

    @Override
    public void updatePlayers(HashMap<Integer, Player> playerlist) {
        
    }

    @Override
    public void updateResult(Integer result) {
        
    }

    @Override
    public void updateReadyNotification(Integer playerId) {
       
    }

    @Override
    public void updateMessage(Message msg) {
        chatArea.appendText(msg.getCompleteMessage()+"\n");
    }

    @Override
    public void updateGameStarting() {
        
    }

    @Override
    public void updateGameOver() {
        
    }

    @Override
    public void reUpdatePlayer(PlayerFix player) {
        
    }

    @Override
    public void updateRoundDoneNotification(Integer playerId) {
        
    }
	@Override
	public void updatePlayerLeft(Integer player) {
		// TODO Auto-generated method stub
		
	}
    
    
}
